/**
 * 
 */
package com.gragra.sequentiaDataGenerator.tests;
import static org.junit.Assert.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.StringReader;
import org.junit.Before;
import org.junit.Test;
import com.gragra.data.Corpus;
import com.gragra.sampling.sequentialStructure.CaseLevel;
import com.gragra.sampling.sequentialStructure.ExtensionInformationHolder;
import com.gragra.sampling.sequentialStructure.ExtensionLevels;
import com.gragra.sampling.sequentialStructure.WordAndTagHolder;
import com.gragra.sampling.sequentialStructure.WordType;
import com.gragra.sequentiaDataGenerator.BaseGenerator;
import com.gragra.sequentiaDataGenerator.CaseLevelAssigningSegmenter;
import com.gragra.sequentiaDataGenerator.PunctuationSplitOffSegmenter;
import com.gragra.util.StringToInt;
/**
 * @author Christoph Teichmann
 * created Mar 26, 2013 9:10:54 PM
 * @version 0.1
 */
public class TestBaseGenerator
{
	/**
	 * 
	 */
	private BaseGenerator bg;
	/**
	 *
	 * @throws java.lang.Exception
	 */
	@Before
	public void setUp() throws Exception
	{bg = new BaseGenerator(new CaseLevelAssigningSegmenter(new PunctuationSplitOffSegmenter("NONE")));}
	/**
	 * Test method for {@link com.gragra.sequentiaDataGenerator.BaseGenerator#createCorpus(java.io.BufferedReader)}.
	 * @throws IOException 
	 */
	@Test
	public final void testCreateCorpus() throws IOException
	{
		StringReader sr = new StringReader("This|0|WordTypeLevel|Noun IS|NONE something|3|ContextTagLevel|10 \n this|Pronoun too.|NONE|WordTypeLevel|Function");
		BufferedReader br = new BufferedReader(sr);
		Corpus<WordAndTagHolder> corp = bg.createCorpus(br);
		WordAndTagHolder wth1 = corp.getEntry(0);
		WordAndTagHolder wth2 = corp.getEntry(1);
		assertEquals(3,wth1.getWords().length);
		assertEquals(3,wth1.getTags().length);
		assertEquals(StringToInt.ensure("this"),wth1.getWord(0));
		assertEquals(StringToInt.ensure("0"),wth1.getTag(0));
		assertEquals(StringToInt.ensure("is"),wth1.getWord(1));
		assertEquals(StringToInt.ensure("3"),wth1.getTag(2));
		assertEquals(-1,wth1.getTag(1));
		assertEquals(3,wth2.getWords().length);
		assertEquals(3,wth2.getTags().length);
		assertEquals(StringToInt.ensure("this"),wth2.getWord(0));
		assertEquals(StringToInt.ensure("Pronoun"),wth2.getTag(0));
		assertEquals(StringToInt.ensure("too"),wth2.getWord(1));
		assertEquals(-1,wth2.getTag(1));
		assertEquals(-1,wth2.getTag(2));
		ExtensionInformationHolder eih = wth1.getExtensionInformation();
		assertEquals(eih.getFiniteInformation(0, ExtensionLevels.WordTypeLevel),WordType.Noun);
		assertEquals(eih.getFiniteInformation(1, ExtensionLevels.WordTypeLevel),null);
		assertEquals(eih.getFiniteInformation(2, ExtensionLevels.WordTypeLevel),null);
		assertEquals(eih.getInfiniteInformation(2, ExtensionLevels.ContextTagLevel),10);
		assertEquals(eih.getInfiniteInformation(0, ExtensionLevels.ContextTagLevel),-1);
		assertEquals(eih.getInfiniteInformation(1, ExtensionLevels.ContextTagLevel),-1);
		assertEquals(eih.getFiniteInformation(2, ExtensionLevels.CaseTypeLevel),CaseLevel.DownCase);
		assertEquals(eih.getFiniteInformation(1, ExtensionLevels.CaseTypeLevel),CaseLevel.CapsLockCase);
		assertEquals(eih.getFiniteInformation(0, ExtensionLevels.CaseTypeLevel),CaseLevel.UpperCase);
		eih = wth2.getExtensionInformation();
		assertEquals(eih.getFiniteInformation(2, ExtensionLevels.WordTypeLevel),null);
		sr = new StringReader("This|0|WordTypeLevel|Noun IS|NONE something|3|ContextTagLevel|10 \n this|Pronoun too.|NONE|WordTypeLevel|Function");
		br = new BufferedReader(sr);
		bg.extendCorpus(br, corp);
		assertEquals(corp.size(),4);
	}
	/**
	 * Test method for {@link com.gragra.sequentiaDataGenerator.BaseGenerator#transferSingle(java.lang.String)}.
	 */
	@Test
	public final void testTransferSingle()
	{
		WordAndTagHolder wta = bg.transferSingle("This|0|WordTypeLevel|Noun IS|NONE something|3|ContextTagLevel|10");
		assertEquals(3,wta.getWords().length);
		assertEquals(3,wta.getTags().length);
		assertEquals(StringToInt.ensure("this"),wta.getWord(0));
		assertEquals(StringToInt.ensure("0"),wta.getTag(0));
		assertEquals(StringToInt.ensure("is"),wta.getWord(1));
		assertEquals(StringToInt.ensure("3"),wta.getTag(2));
		assertEquals(-1,wta.getTag(1));
		ExtensionInformationHolder eih = wta.getExtensionInformation();
		assertEquals(eih.getFiniteInformation(0, ExtensionLevels.WordTypeLevel),WordType.Noun);
		assertEquals(eih.getFiniteInformation(1, ExtensionLevels.WordTypeLevel),null);
		assertEquals(eih.getFiniteInformation(2, ExtensionLevels.WordTypeLevel),null);
		assertEquals(eih.getInfiniteInformation(2, ExtensionLevels.ContextTagLevel),10);
		assertEquals(eih.getInfiniteInformation(0, ExtensionLevels.ContextTagLevel),-1);
		assertEquals(eih.getInfiniteInformation(1, ExtensionLevels.ContextTagLevel),-1);
		assertEquals(eih.getFiniteInformation(2, ExtensionLevels.CaseTypeLevel),CaseLevel.DownCase);
		assertEquals(eih.getFiniteInformation(1, ExtensionLevels.CaseTypeLevel),CaseLevel.CapsLockCase);
		assertEquals(eih.getFiniteInformation(0, ExtensionLevels.CaseTypeLevel),CaseLevel.UpperCase);
	}

	@Test
	public final void transferToString()
	{
		WordAndTagHolder wta = bg.transferSingle("This|0 IS|NONE something|3");
		String s = BaseGenerator.transferToString(wta);
		assertEquals("this|0|CaseTypeLevel|UpperCase is|NONE|CaseTypeLevel|CapsLockCase something|3|CaseTypeLevel|DownCase",s);
	}
}